<template>
  <div class="backStack-issue-wrap">
    <TitleCard :title="titleCard" :showMore="showMore" v-if="userType">
      <UserInfos ref="userInfos" :infos="userInfo"></UserInfos>
    </TitleCard>
    <TitleCard :title="titleCardTwo" :showMore="showMore" v-else>
      <UserInfos ref="userInfosTwo" :infos="companyInfo"></UserInfos>
    </TitleCard>
  </div>
</template>

<script>
import mixinsBackStack from '@/views/backstageSupport/mixins'
import { userInfo, companyInfo } from './configs/configs';
import UserInfos from '../../components/userInfos/index.vue'
import { getAccountDetail } from '@/api/accountInfos.js'

export default {
  mixins: [mixinsBackStack],
  data() {
    return {
      userInfo,
      companyInfo,
      titleCardTwo: '公司信息',
      defaultModel: {},
      defaultModelTwo: {},
      userType: false
    }
  },
  components: {
    UserInfos
  },
  created() {
    this.initSetting()  // 初始化默认配置
  },
  methods: {
    // 初始化数据
    async initSetting() {
      const { id, userType } = this.$route.query
      this.userType = userType === '01' || userType === '00' ? true : false
      const { data } = await getAccountDetail(id)
      this.handlerResultMappings([data]) // 处理数据的映射关系
      this.handlerFileAndImg(data)
      data.address = data.province + data.city + data.area //个人信息地址
      data.enpAllAdress = data.province + data.city + data.area + data.enpAddress //企业信息地址
      this.dealInfo('userInfo', data)
      this.dealInfo('companyInfo', data)
    },
    // 处理静态信息
    dealInfo(type, result) {
      if (!result) return
      if (!this[type]) return
      this[type].forEach(item => {
        const { key } = item
        const v = result[key] || '-'
        item.value = v
      })
    },
    // 处理附件和附图的数据
    handlerFileAndImg(data) {
      const { logo, enpPic } = data
      if (logo) {
        data.logo = `<img style="width:100px; height:100px; " src="${logo}" alt="${logo}">`
      }
      if (enpPic) {
        data.enpPic = `<img style="width:100px; height:100px; " src="${enpPic}" alt="${enpPic}">`
      }
    },
    // 处理富文本
    handlerRichText(result) {
      const key1 = 'content'
      const key2 = 'section'
      Object.keys(result).forEach(key => {
        const isKey1 = key.indexOf(key1) !== -1
        const isKey2 = key.indexOf(key2) !== -1
        if (isKey1 || isKey2) {
          const len = key.length
          const idx = key.slice(-1)
          const ctx = key.substring(0, len - 1)
          const k = `${idx}-${ctx}`
          result[k] = result[key]
        }
      })
      return result
    },
  }
}
</script>
